const sys = require(`./system.js`)
const $ = sys.$('助学资产转移')

sys.run($, 'task:tran', (resList, cookie, index, username, password, item) => {
    return new Promise(async resolve => {
        // 请求接口
        // console.log('item', item)
        const qyId = item.qyId
        const targetId = item.targetId
        
        // 1.大号转小号权益
        const resp1 = await $.publicJS.bidTransfer($, cookie, qyId, targetId, undefined)
        console.log('1.大号转小号权益', resp1)

        // 2.查询大号权益转卖ID
        const quanyi = (await $.publicJS.transferList($, cookie))[0] || {}
        const id = quanyi.id
        console.log('2.查询大号权益转卖ID', id)

        // 3.小号购买转卖权益
        const targetUsername = item.targetUsername
        const targetcookie = await $.publicJS.checkCookie($, $.localStorage.get(`${targetUsername}-cookie`, 'localStorage'), targetUsername, password)
        if(id) {
          const resp3 = await $.publicJS.buyTransferCode($, targetcookie, id)
          console.log('3.小号购买转卖权益', resp3)
        } else {
          console.log('3.小号购买转卖权益 空')
        }

        // 4.小号转大号权益
        const rate = item.rate
        const resp4 = await $.publicJS.bidTransfer($, targetcookie, qyId, undefined, rate)
        console.log('4.小号转大号权益', resp4)
       

        // 5.查询小号权益转卖ID
        const quanyi2 = (await $.publicJS.transferList($, targetcookie))[0] || {}
        const id2 = quanyi2.id
        console.log('5.查询小号权益转卖ID', id2)

        // 6.大号购买转卖权益
        if(id2) {
          const resp6 = await $.publicJS.buyTransferCode($, cookie, id2)
          console.log('6.大号购买转卖权益', resp6)
        } else {
          console.log('6.大号购买转卖权益 空')
        }
        
        resolve(resList)
    })
}, (resList) => {
    console.log('任务结束 resList:', resList)
})
